package com.sky.task;


import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.Schedules;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @ClassName: OrderTask
 * @Author: YuanDing
 * @Date: 2024/2/8 20:44
 * @Description: 订单定时任务
 */

@Slf4j
@Component
public class OrderTask {

    @Autowired
    private OrderMapper orderMapper;

    @Scheduled( cron = "0 */1 * * * ?")
    private void clearTimeOutOrder() {
        LocalDateTime time = LocalDateTime.now().plusMinutes(-15);
        log.info("开始执行清理超时订单任务");
        // 查询超时的订单
        List<Orders>  ordersList = orderMapper.selectTimeOutOrder(time);

        if (ordersList != null && ordersList.size() > 0) {
            ordersList.forEach( item -> {
                item.setCancelReason("订单超时");
                item.setStatus(Orders.CANCELLED);
                item.setCancelTime(LocalDateTime.now());
            });
            // 批量更新订单
            orderMapper.updateBatch(ordersList);
            log.info("清理超时订单: {}" , ordersList);
        }

    }

}
